package com.project.api.ym.service;

import com.project.api.ym.dao.UserDao;
import com.project.api.ym.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
    @Autowired
    private UserDao userDao;
    public List GetUser(Map condition, int offset, int limit, String orderby){
        Iterator<Map.Entry<String, Object>> entries = condition.entrySet().iterator();
        StringBuilder q = new StringBuilder();
        while(entries.hasNext()){
            Map.Entry<String, Object> entry = entries.next();
            String key = entry.getKey();
            Object value = entry.getValue();
            q.append(" and ");
            if(key.equalsIgnoreCase("stime")||key.equalsIgnoreCase("gtime")){
                q.append("`").append(key).append("`>=").append("'").append(value).append("'");
            }else if(key.equalsIgnoreCase("etime")||key.equalsIgnoreCase("otime")){
                q.append("`").append(key).append("`<=").append("'").append(value).append("'");
            }else if(key.equalsIgnoreCase("nick_name")||key.equalsIgnoreCase("user_name")||key.equalsIgnoreCase("phone")||key.equalsIgnoreCase("company")){
                q.append("`").append(key).append("`").append(" like '%").append(value).append("%'");
            }
            else{
                q.append("`").append(key).append("`=").append("'").append(value).append("'");
            }
        }
        String qs = q.toString();
        String where = "";
        if(!qs.equalsIgnoreCase("")){
//            where= qs.substring(0,qs.length()-1);
            where=qs;
        }
        if (orderby!=null) where = where + " order by "+orderby;
        where = where + " limit "+ offset + ","+limit;
        return userDao.GetUser(where);
    }

    public int GetUserCount(Map condition){
        Iterator<Map.Entry<String, Object>> entries = condition.entrySet().iterator();
        StringBuilder q = new StringBuilder();
        while(entries.hasNext()){
            Map.Entry<String, Object> entry = entries.next();
            String key = entry.getKey();
            Object value = entry.getValue();
            q.append(" and ");
            if(key.equalsIgnoreCase("stime")||key.equalsIgnoreCase("gtime")){
                q.append("`").append(key).append("`>=").append("'").append(value).append("'");
            }else if(key.equalsIgnoreCase("etime")||key.equalsIgnoreCase("otime")){
                q.append("`").append(key).append("`<=").append("'").append(value).append("'");
            }else if(key.equalsIgnoreCase("nick_name")||key.equalsIgnoreCase("user_name")||key.equalsIgnoreCase("phone")||key.equalsIgnoreCase("company")){
                q.append("`").append(key).append("`").append(" like '%").append(value).append("%'");
            }
            else{
                q.append("`").append(key).append("`=").append("'").append(value).append("'");
            }
        }
        String qs = q.toString();
        String where = "";
        if(!qs.equalsIgnoreCase("")){
            where = qs;
//            where=qs.substring(0,qs.length()-1);
        }
        return userDao.GetUserCount(where);
    }

    public User GetUserById(long id){
        return userDao.GetUserById(id);
    }
}
